<?php
/**
 * @author wyl
 * @date 2015-8-26 11:15:29
 */
Yii::import('system.web.widgets.CWidget');

class CAddWatchList extends CWidget
{
    public $label;
    public $w_url;
    public $c_url;
    public $auctionId;
    public $class;

    public function init()
    {
        $this->label = (empty($this->label)) ? 'ADD TO WATCH LIST' : $this->label;
        $this->class = (empty($this->class)) ? 'btn btn-default btn-watch-list add-watch-list' :$this->class;
        $this->w_url=Yii::app()->baseUrl.'/ajax/ajaxwatch'; 
        $this->c_url=Yii::app()->baseUrl.'/ajax/ajaxcancelwatch';
        
    }

    public function run()
    {   
        $this->iswatch();
        $this->renderButton();
        $this->addScript();
    }

    public function renderButton()
    {
        echo CHtml::htmlButton($this->label, array(
            'class' => $this->class,
        ));
    }
    public function iswatch(){
        $favorites=new Favorites();
        $favorites_data=$favorites->findByAttributes(array('customer_id'=>Yii::app()->user->id,'auction_id' => $this->auctionId));
        if(!empty($favorites_data)){
            if($favorites_data->enable==1){
            $this->label='Remove';
            $this->class='btn btn-default btn-watch-list delete-watch-list';
            }
        }
        
        return $this;
    }
    public function addScript()
    {   
        if(!Yii::app()->user->isGuest){

        
            Yii::app()->clientScript->registerScript('add-watch-list-js', "
                $(document).on('click','.add-watch-list',function(){
                    var ajaxdata={'auctionId':{$this->auctionId}};
                    $.ajax({
                        url:'{$this->w_url}',
                        data:ajaxdata,
                        type:'post',
                        datatype:'json',
                        success:function(data){
                            if(data.hasOwnProperty('error')){
                                alert(data.error);
                            }else{
                                $('.add-watch-list').each(function(){
                                    $(this).html('Remove');
                                    $(this).removeClass('add-watch-list').addClass('delete-watch-list');
                                })             
                            }
                        }


                    })
                    
                });
                $(document).on('click','.delete-watch-list',function(){
                    var ajaxdata={'auctionId':{$this->auctionId}};
                    $.ajax({
                        url:'{$this->c_url}',
                        data:ajaxdata,
                        type:'post',
                        datatype:'json',
                        success:function(data){
                            if(data.hasOwnProperty('error')){
                                alert(data.error);
                            }else{
                                    $('.delete-watch-list').each(function(){
                                        $(this).html('ADD TO WATCH LIST');
                                        $(this).removeClass('delete-watch-list').addClass('add-watch-list');
                                    })
                            }
                        }
                    })       
                    
                });
    		");
        }
    }

}		
